package com.zhkj.txg.module.home.adapter

import android.view.View
import com.chad.library.adapter.base.BaseNodeAdapter
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.ShopCateSecondEntity
import com.zhkj.txg.module.home.entity.ShopCategoryEntity
import com.zhkj.txg.module.home.ui.ProductListActivity

class ShopCateAdapter(
    private val shopName: String = "",
    private val shopId: Long = 0
) : BaseNodeAdapter() {

    companion object {
        val TYPE_SECOND = 0
        val TYPE_THIRD = 1
    }

    init {
        addFullSpanNodeProvider(ShopSecondProvider())
        addNodeProvider(ShopThirdProvider(shopName, shopId))
    }

    override fun getItemType(data: List<BaseNode>, position: Int): Int {
        return when (data[position]) {
            is ShopCategoryEntity -> TYPE_SECOND
            else -> TYPE_THIRD
        }
    }
}

class ShopSecondProvider : BaseNodeProvider() {
    override val itemViewType: Int = ShopCateAdapter.TYPE_SECOND
    override val layoutId: Int = R.layout.item_shop_cate_second

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? ShopCategoryEntity)?.let {
            helper.setText(R.id.tvShopCateName, data.name)
        }
    }
}

class ShopThirdProvider(
    private val shopName: String = "",
    private val shopId: Long = 0
) : BaseNodeProvider() {
    override val itemViewType: Int = ShopCateAdapter.TYPE_THIRD
    override val layoutId: Int = R.layout.item_shop_cate_third

    override fun convert(helper: BaseViewHolder, data: BaseNode) {
        (data as? ShopCateSecondEntity)?.let {
            helper.setText(R.id.tvShopCateThird, data.name)
        }
    }

    override fun onClick(helper: BaseViewHolder, view: View, data: BaseNode, position: Int) {
        (data as? ShopCateSecondEntity)?.let {
            ProductListActivity.shopOpen(context, it.name, shopId.toInt(), it.id)
        }
    }
}